package com.bst.bsj.bops.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.bst.bsj.base.controller.WebBaseController;
import com.bst.bsj.bops.web.biz.AreaService;
import com.bst.bsj.bops.web.biz.MemberService;
import com.bst.bsj.bops.web.biz.SeedlingCompanyService;
import com.bst.bsj.bops.web.biz.SeedlingService;
import com.bst.bsj.bops.web.biz.SeedlingWareHouseService;
import com.bst.bsj.bops.web.biz.SnsFollowForCompanyService;
import com.bst.bsj.bops.web.biz.SnsFollowForSeedlingService;
import com.bst.bsj.bops.web.biz.SubAccountService;
import com.bst.bsj.bops.web.biz.UserMessageService;
import com.bst.bsj.common.dal.dataobject.Area;
import com.bst.bsj.common.dal.dataobject.Member;
import com.bst.bsj.common.maopu.enums.MemberStatus;
import com.homewaimai.common.lang.Paginator;
import com.homewaimai.common.lang.Result;
import com.homewaimai.common.lang.StringUtil;

@Scope("prototype")
@Controller
@RequestMapping(value = "/bops/sys/member")
public class MemberController extends WebBaseController {

  @Autowired
  private MemberService memberService;
  @Autowired
  private AreaService areaService;
  @Autowired
  private SeedlingService seedlingService;
  @Autowired
  private SeedlingCompanyService seedlingCompanyService;
  @Autowired
  private SubAccountService subAccountService;
  @Autowired
  private SnsFollowForSeedlingService snsFollowForSeedlingService;
  @Autowired
  private SnsFollowForCompanyService snsFollowForCompanyService;
  @Autowired
  private SeedlingWareHouseService seedlingWareHouseService;
  @Autowired
  private UserMessageService userMessageService;

  
  /**
   * 
  * @Title: auditList
  * @Description: TODO(企业审核管理列表)
  * @param req
  * @param resp
  * @return
  * @throws Exception
  * ModelAndView
  * @throws 
  * @creator     :tonglu
  * @create date :2016年5月13日 上午9:37:34
  * @modificator :tonglu
  * @modify date :2016年5月13日 上午9:37:34
   */
  @SuppressWarnings("unchecked")
  @RequestMapping(value = "/memberList")
  public ModelAndView auditList(HttpServletRequest req, HttpServletResponse resp) throws Exception {
    Map<String, Object> params = getBopsParams(req);//参数
    Map<String, Object> paramsMap = new HashMap<String, Object>();
    ModelAndView modelAndView = getBopsModelAndView("company/user");
    String realName = (String) params.get("realName");
    String provinceName = (String) params.get("provinceName");
    String cityName = (String) params.get("cityName");
    String mobile = (String) params.get("mobile");
    String status = (String) params.get("status");
    if (realName != null) {
      realName = realName.trim();
    }
    if ("全部".equals(status)) {
      status = "";
    }

    if (mobile != null) {
      mobile = mobile.trim();
    }
    String pageNums = (String) params.get("pageNum");
    Integer pageNum = 0;
    if (StringUtil.isNotBlank(status)) {
      paramsMap.put("status", status);
    }
    if (StringUtil.isNotBlank(realName)) {
      paramsMap.put("realName", realName);
    }
    if (StringUtil.isNotBlank(provinceName)) {
      paramsMap.put("provinceName", provinceName);
    }
    if (StringUtil.isNotBlank(cityName)) {
      paramsMap.put("cityName", cityName);
    }
    if (StringUtil.isNotBlank(mobile)) {
      paramsMap.put("mobile", mobile);
    }
    if (StringUtil.isNumeric(pageNums)) {
      pageNum = Integer.valueOf(pageNums);
    }
    if (pageNum == null || pageNum == 0) {
      pageNum = 1;
    }
    paramsMap.put("page", pageNum);
    paramsMap.put("length", new Long(10));
    paramsMap.put("offset", new Long((pageNum - 1) * 10));
    Result result = memberService.query(paramsMap);
    // 如果请求成功
    if (result.isSuccess()) {
      // 得到分页器
      Paginator paginator = (Paginator) result.getModels().get("paginator");
      // 得到数据
      List<Member> members = (List<Member>) result.getModels().get("members");
      modelAndView.addObject("members", members);
      modelAndView.addObject("paginator", paginator);
    }
    modelAndView.addObject("params", params);
    
    List<Area> provinceList = areaService.getAreas("province"); //所有省
    modelAndView.addObject("provinceList", provinceList);
    
    return modelAndView;
  }

  /**
   * 
  * @Title: getSeedlingCompany
  * @Description: TODO(根据id修改状态)
  * @param req
  * @param resp
  * @param id
  * @return
  * @throws Exception
  * ModelAndView
  * @throws 
  * @creator     :tonglu
  * @create date :2016年5月13日 下午3:14:01
  * @modificator :tonglu
  * @modify date :2016年5月13日 下午3:14:01
   */
  @RequestMapping(value = "/changeStatus")
  public void changeStatus(HttpServletRequest req, HttpServletResponse resp, String id) throws Exception {
    Map<String, Object> params = getBopsParams(req);//参数
    String status = (String) params.get("status");//状态

    if(MemberStatus.CANCEL.getCode().equals(status)){ //注销
      String uid = (String) params.get("uid");
      String mobile = (String) params.get("mobile");
      Map<String, Object> paramsMap =new HashMap<String,Object>();
      paramsMap.put("uid", uid);
      paramsMap.put("mobile", mobile);
      
    //注销时删除相关的数据  
     subAccountService.deleteBySeedlingMobile(paramsMap);
     subAccountService.deleteBySeedlingCompany(paramsMap);
     
     snsFollowForSeedlingService.deleteByParams(paramsMap);
     snsFollowForSeedlingService.deleteBySeedling(paramsMap);
     
     snsFollowForCompanyService.deleteByParams(paramsMap);
     snsFollowForCompanyService.deleteBySeedlingCompany(paramsMap);
     
     seedlingWareHouseService.deleteByParams(paramsMap);
     userMessageService.deleteByParams(paramsMap);
      
     seedlingService.deleteByParams(paramsMap);
     seedlingCompanyService.deleteByParams(paramsMap);
     memberService.deleteByParams(paramsMap);
     
     
      
    }else{  //封号
      Member member = new Member();
      member.setId(Long.valueOf(id));
      member.setStatus(status);
      memberService.update(member);
    }
    
  }
}
